package com.example.exercise.appmanage.system.service.impl;

import com.example.exercise.appmanage.system.dto.request.MenuRequest;
import com.example.exercise.appmanage.system.dto.response.MenuResponse;
import com.example.exercise.appmanage.system.mapper.MenuMapper;
import com.example.exercise.appmanage.system.mapper.entity.MenuEntity;
import com.example.exercise.appmanage.system.service.MenuService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Service
public class MenuServiceImpl implements MenuService {
    @Resource
    private MenuMapper menuMapper;

    @Override
    public MenuEntity findMenuById(int id) {
        return menuMapper.getMenuById(id);
    }

    @Override
    public void modifyMenu(MenuRequest menuRequest) {
        MenuEntity menuEntity = new MenuEntity();
        BeanUtils.copyProperties(menuRequest,menuEntity);
        menuMapper.modifyMenu(menuEntity);
    }

    @Override
    public boolean dropMenuById(int id) {
        MenuEntity menuEntity = menuMapper.getMenuById(id);
        String menuparentid = menuEntity.getMenuid();
        List<MenuEntity> menuEntities = menuMapper.getMenuListByParentId(menuparentid);
        if(menuEntities.isEmpty()){
            menuMapper.deleteById(id);
            return true;
        }
        return false;
    }

    @Override
    public void saveMenu(MenuRequest menuRequest) {
        String menuId = generateMenuId(menuRequest.getMenuparentid());
        MenuEntity menuEntity = new MenuEntity();
        BeanUtils.copyProperties(menuRequest,menuEntity);
        menuEntity.setMenuid(menuId);
        if(!StringUtils.hasText(menuEntity.getMenuurl())){
            menuEntity.setMenuurl("default.html");
        }
        menuMapper.saveMenu(menuEntity);
    }

    @Override
    public List<MenuResponse> getMenuList() {
        List<MenuResponse> menuResponses = new ArrayList<>();
        List<MenuEntity> list = menuMapper.getMenuListByParentId("000000");
        for (MenuEntity menuEntity : list) {
            MenuResponse menuResponse = new MenuResponse();
            BeanUtils.copyProperties(menuEntity,menuResponse);
            List<MenuResponse> menuResponseList = new ArrayList<>();
            List<MenuEntity> subList = menuMapper.getMenuListByParentId(menuEntity.getMenuid());
            menuResponse.setChildnode(subList.isEmpty()? 0:1);
            for (MenuEntity menuEntity1: subList) {
                MenuResponse menuResponse1 = new MenuResponse();
                BeanUtils.copyProperties(menuEntity1,menuResponse1);
//                List<MenuEntity> subList1 = menuMapper.getMenuListByParentId(menuEntity1.getMenuid());
//                menuResponse1.setChildnode(subList1.isEmpty()? 0:1);
                menuResponseList.add(menuResponse1);
            }
            menuResponse.setMenuResponses(menuResponseList);
            menuResponses.add(menuResponse);
        }
        return menuResponses;
    }

    private String generateMenuId(String parentMenuId){
        String first = parentMenuId.substring(0,1);
        Integer firstNum = Integer.valueOf(first);
        String firstStr = String.valueOf(firstNum + 1);
        String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        int length = str.length();
        StringBuilder stringBuilder = new StringBuilder(firstStr);
        for (int i = 0; i < 5; i++) {
            stringBuilder.append(str.charAt(new Random().nextInt(length)));
        }
        String menuId = stringBuilder.toString();
        MenuEntity menuEntity = menuMapper.getByMenuid(menuId);
        if(menuEntity == null){
            return menuId;
        }else{
            return generateMenuId(parentMenuId);
        }
    }
}
